Now, let's talk
about deployments. Let's talk about the
process you use to update your application
on Cloud Run. Change your application
source code, build and package
your application into a container image, push the container image
to Artifact Registry, deploy to the service. Once Cloud Run has
deployed your changes, which may entail a short weight, you will be given
an endpoint URL to access the application. First, let's take a
look in more detail at the service
configuration on Cloud Run. Changing any of these
settings makes Cloud Run deploy a new revision
of your application. This is how you should
think about a deployment. You're editing a
resource/document. Cloud Run picks up the change, creates a new revision, and immediately sends traffic to the new revision as
soon as it is healthy. The service resource
describes how Cloud Run runs your application. We can use the
analogy of a recipe. The service resource is the recipe for your
application with all the ingredients
and instructions about how to cook a
running application. Cloud Run deploys your
application after every change you make to
the service resource. At the same time, it also makes an
immutable copy of the service resource
called a revision. Immutable means that you can't make changes
to a revision. You can only add new
revisions to make updates. To state this in another way, a revision is a
unique combination of the contents of a
container image and its service configuration. It is called immutable
because once created, it cannot be edited. It can only be replaced
by another version. A revision is a copy of
the service resource, and it is also
exists at run time. If Cloud Run sees a
new revision resource, it will create a new
revision at run time, which is a group of containers
you can send requests to. Cloud Run waits for the first container in that
revision to finish starting. When Cloud Run is waiting, the old or current revision still says production traffic. Now, as soon as the first container in the
new revision is ready, Cloud Run thinks it is ready
to accept a TCP connection. Cloud Run sends all
production traffic to it. The container in the
previously active revision will become idle. That is, it won't
serve requests. If something goes wrong, the current revision
continues to serve production traffic as
if nothing happened. However, be aware that
Cloud Run does not look at the response codes to determine if a container is healthy. If the new revision is
healthy, both revisions, the old revision, and the new revision,
automatically scale independently. The new revision might have
to add more containers to handle demand in this
example from 1-3. The old revision will eventually remove the idle containers
and scale to zero. If your application
handles a lot of traffic and takes
some time to start, this scaling behavior
might not work well, and resulting request queuing. There is a solution. You can gradually
migrate traffic, and I'll come back to that soon. Solutions like pre-warming a
revision are not available. After you edit the
service resource, Cloud Run copies the
service configuration to a new resource, creates a new
revision at runtime, waits for the first
container to become ready, shifts all traffic
to the new revision, auto-scale both the old
and the new revision. By default, Cloud Run sends all traffic to the
latest healthy revision. I like to use this
analogy of a magnet. It snaps to a new
revision as soon as the first container
accepts a TCP connection. Latest is a magnet. It snaps only to
a ready revision. Remember, ready means
that the container is able to serve HTTP requests. You can also pin traffic to a specific revision rather
than the latest revision, decoupling deployment
of a new revision from the migration of traffic. This also means that if
you add a new revision, Cloud Run will not automatically send traffic to
that new revision. There is no magnet that
snaps back automatically. Pinning to a revision
is useful if you want to roll back to
a previous revision. Or, if you first want to test your new revision before
moving all traffic. I'll come back to how to
preview a new revision later. Cloud Run also lets
you tag a revision. A tagged revision has
its own unique URL. It's the URL of the
Cloud Run service with the name of the tag
added as a prefix. For example, you might want to tag all revisions
automatically with the ID of the commit
inversion control that was used to
create the revision. A tag can also be used to point to the
latest healthy revision. This is how you can support a preview linked to
the latest revision. You run a few confidence
checks before migrating production traffic
over to the new revision. Tags can also send traffic
to one revision at a time. Traffic splitting over multiple revisions
is not supported. Tags allow you to
test or preview a diploid revision before
assigning it to any traffic. This allows thorough
testing before a new revision is
used for production. To accomplish this
type of testing, you run integration tests on your container
during development. Deploy your container to a Google Cloud Project that
you use only for staging, meaning it serves no traffic and test against a
tapped revision. Deploy the container to production once again
without allowing it to serve traffic
and test against a tagged revision in production. Finally, migrate traffic to the tagged revision once
your tests are complete. Now, you might not want to deploy all traffic immediately. At Google, all of our
deployments are gradual, and they might take days
to get to 100 percent. This allows us to
carefully monitor for increased error rates and
find issues with scaling. You're not limited to using two revisions in
a traffic split, although it is the most
common way to use them. If you configure
a traffic split, requests are distributed
on a percentage basis, and they are not sticky. Request for one client may
land on both active revisions. To summarize, the tag latest always points to the
latest ready revision. By default, traffic will automatically flow
to that revision. You can also set
up traffic splits between one or more
revisions using tags. Tagged revisions have
a unique URL and can even point to the latest
revision of your application.